package org.dromara.tearoom.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.tearoom.domain.bo.TerGroupBuyAppBo;
import org.dromara.tearoom.domain.bo.TerGroupBuyBo;
import org.dromara.tearoom.domain.vo.TerGroupBuyTotalVo;
import org.dromara.tearoom.domain.vo.TerGroupBuyVo;
import org.dromara.tearoom.service.ICertificateCouponService;
import org.dromara.tearoom.service.ITerGroupBuyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 团购核销
 * 前端访问路由地址为:/tearoom/groupBuy
 *
 * @author ckj
 * @date 2025-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/groupBuy")
public class TerGroupBuyController extends BaseController {

    private final ITerGroupBuyService terGroupBuyService;
    private final ICertificateCouponService certificateCouponService;

    /**
     * 查询团购核销列表
     */
    @SaCheckPermission("tearoom:groupBuy:list")
    @GetMapping("/list")
    public TableDataInfo<TerGroupBuyVo> list(TerGroupBuyBo bo, PageQuery pageQuery) {
        return terGroupBuyService.queryPageList(bo, pageQuery);
    }

    /**
     * 统计团购核销数量
     */
    @PostMapping("/total")
    public R<List<TerGroupBuyTotalVo>> total(@RequestBody TerGroupBuyBo bo) {
        return R.ok(terGroupBuyService.total(bo));
    }

    /**
     * 导出团购核销列表
     */
    @SaCheckPermission("tearoom:groupBuy:export")
    @Log(title = "团购核销", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerGroupBuyBo bo, HttpServletResponse response) {
        List<TerGroupBuyVo> list = terGroupBuyService.queryList(bo);
        ExcelUtil.exportExcel(list, "团购核销", TerGroupBuyVo.class, response);
    }

    /**
     * 获取团购核销详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tearoom:groupBuy:query")
    @GetMapping("/{id}")
    public R<TerGroupBuyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(terGroupBuyService.queryById(id));
    }

    /**
     * 新增团购核销
     */
    @SaCheckPermission("tearoom:groupBuy:add")
    @Log(title = "团购核销", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerGroupBuyBo bo) {
        return toAjax(terGroupBuyService.insertByBo(bo));
    }

    /**
     * 修改团购核销
     */
    @SaCheckPermission("tearoom:groupBuy:edit")
    @Log(title = "团购核销", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerGroupBuyBo bo) {
        return toAjax(terGroupBuyService.updateByBo(bo));
    }

    /**
     * 删除团购核销
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:groupBuy:remove")
    @Log(title = "团购核销", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terGroupBuyService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 准备验劵
     *
     * @param bo 主键串
     */
    @PostMapping("/certificatePrepare")
    @RepeatSubmit
    public R<List<TerGroupBuyVo>> certificatePrepare(@Validated(AddGroup.class) @RequestBody TerGroupBuyAppBo bo) {
        return R.ok(certificateCouponService.certificatePrepare(bo));
    }

    /**
     * 核销验劵
     *
     * @param bo 主键串
     */
    @PostMapping("/certificate")
    @RepeatSubmit
    public R<Void> certificate(@Validated(AddGroup.class) @RequestBody TerGroupBuyAppBo bo) {
        return toAjax(certificateCouponService.certificate(bo));
    }



}
